package com.xxxx.business.controller.admin;


import com.xxxx.server.dto.ChapterDto;
import com.xxxx.server.dto.ChapterPageDto;
import com.xxxx.server.dto.ResponseDto;
import com.xxxx.server.service.ChapterService;
import com.xxxx.server.util.ValidatorUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/chapter")
public class ChapterController {

    public static final String BUSINESS_NAME = "大章";

    @Resource
    private ChapterService chapterService;

    @PostMapping("/list")
    public ResponseDto list(@RequestBody ChapterPageDto chapterPageDto) {
        ResponseDto<ChapterPageDto> responseDto = new ResponseDto<>();
        //  必须得有课程id
        ValidatorUtil.require(chapterPageDto.getCourseId(), "课程ID");
        chapterService.list(chapterPageDto);
        responseDto.setContent(chapterPageDto);
        return responseDto;
    }

    @PostMapping("/save")
    public ResponseDto save(@RequestBody ChapterDto chapterDto) {
        //  保存校验
        ValidatorUtil.require(chapterDto.getName(), "名称");
        ValidatorUtil.require(chapterDto.getCourseId(), "课程ID");
        ValidatorUtil.length(chapterDto.getCourseId(), "课程ID", 1, 8);

        ResponseDto<ChapterDto> responseDto = new ResponseDto<>();

        chapterService.save(chapterDto);
        responseDto.setContent(chapterDto);
        return responseDto;
    }

    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto<ChapterDto> responseDto = new ResponseDto<>();

        chapterService.delete(id);
        return responseDto;
    }
}
